package com.ruoyi.web.controller.shop.mapper;

import com.ruoyi.common.core.dao.BaseMapper;
import com.ruoyi.common.entity.BO.wechat.AuthedAppletBO;
import com.ruoyi.common.entity.DO.shop.ShopAppletDO;
import com.ruoyi.common.entity.VO.shop.ShopAppletSearchVO;
import com.ruoyi.common.entity.VO.shop.ShopAppletVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface IBackShopAppletMapper extends BaseMapper<ShopAppletDO> {

    List<ShopAppletVO> searchList(ShopAppletSearchVO shopAppletSearchVO);

    /**
     * 根据店铺小程序ID查询信息
     *
     * @param shopAppletId 店铺
     * @return 店铺信息
     */
    ShopAppletVO getShopAppletInfoById(Long shopAppletId);

    /**
     * 删除店铺
     *
     * @param shopAppletId 店铺ID
     * @return 结果
     */
    @Update("UPDATE shop_applet SET is_delete = 1 WHERE id = #{id}")
    int deleteShopAppletById(Long shopAppletId);

    /**
     * 根据店铺小程序名称查询店铺ID
     */
    @Select("SELECT id FROM shop_applet WHERE applet_name=#{appletName} AND is_delete=0")
    Long getIdByShopAppletName(String appletName);

    /**
     * 根据APPID查询信息
     *
     * @param appId appId
     * @return 店铺信息
     */
    @Select("SELECT sa.applet_app_id appId,sa.applet_name appletName,sa.shop_id shopId,sa.release_version releaseVersion,sa.release_desc releaseDesc,sa.release_time releaseTime" +
            ",s.shop_name shopName,s.shop_logo shopLogo" +
            ",sac.id commitId,sac.user_version commitVersion,sac.user_desc commitDesc,sac.user_version auditVersion,sac.demo_qr_code demoQrCode,sac.create_time commitCreateTime,sac.status" +
            ",sasa.auditid,sasa.create_time auditCreateTime" +
            "  FROM shop_applet sa" +
            " LEFT JOIN shop s on sa.shop_id=s.id" +
            " LEFT JOIN shop_applet_commit sac on sa.shop_id=sac.shop_id" +
            " LEFT JOIN shop_applet_submit_audit sasa on sac.id=sasa.commit_id" +
            " WHERE sa.applet_app_id=#{appId} AND sa.is_delete=0 AND sa.is_enable=1 AND s.is_enable=1 AND s.is_delete=0 " +
            " ORDER BY sasa.create_time desc,sac.create_time desc,s.create_time desc" +
            " LIMIT 1"
    )
    AuthedAppletBO getShopAppletAuthInfoById(String appId);


    @Update("UPDATE shop_applet SET review_status = #{status} WHERE applet_app_id = #{appId}")
    int updateReviewStatusByAppId(@Param("appId") String appId, @Param("status") Integer status);

    @Update("UPDATE shop_applet SET is_auth = #{isAuth} WHERE applet_app_id = #{appId}")
    int updateIsAuthByAppId(@Param("isAuth") Integer isAuth, @Param("appId") String appId);

    @Update("UPDATE shop_applet SET is_auth = 0")
    int resetIsAuth();

}
